<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable" >
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:info-standard-line',
                tooltip: '查看帖子详情',
                onClick: handleView.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除此帖子',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, reactive } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
    import { getPostList,deletePost } from '/@/api/demo/bbs';
  import { PageWrapper } from '/@/components/Page';

  import { useModal } from '/@/components/Modal';
  import AccountModal from './AccountModal.vue';

  import { columns } from './post.data';
  import { useGo } from '/@/hooks/web/usePage';

  export default defineComponent({
    name: 'PostManagement',
    components: { BasicTable, PageWrapper, AccountModal, TableAction },
    setup() {
      const go = useGo();
      const [registerModal, { openModal }] = useModal();
      // const searchInfo = reactive<Recordable>({});
      const [registerTable, { reload, updateTableDataRecord }] = useTable({
        title: '帖子列表',
        api: getPostList,
        rowKey: 'id',
        columns,
        // formConfig: {
        //   labelWidth: 120,
        //   schemas: searchFormSchema,
        //   autoSubmitOnEnter: true,
        // },
        useSearchForm: false,
        showTableSetting: true,
        bordered: true,
        // handleSearchInfoFn(info) {
        //   console.log('handleSearchInfoFn', info);
        //   return info;
        // },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          // slots: { customRender: 'action' },
        },
      });

      function handleDelete(record: Recordable) {
        deletePost(record.id).then(res=>{
          reload();
        })    
      }

      function handleSuccess({ isUpdate, values }) {
        if (isUpdate) {
          // 演示不刷新表格直接更新内部数据。
          // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
          const result = updateTableDataRecord(values.id, values);
          console.log(result);
        } else {
          reload();
        }
      }

      function handleView(record: Recordable) {
        // go('/system/account_detail/' + record.id);
        window.open('http://localhost:8009/bbs/post/'+record.id)
      }

      return {
        registerTable,
        registerModal,
        handleDelete,
        handleSuccess,
        // handleSelect,
        handleView,
      };
    },
  });
</script>
